﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using AutoMapper;

namespace Data
{
    
   public class ChiTietBaoCaoCongNoRepository
    {
       private static ChiTietBaoCaoCongNoDTO MapChiTietBaoCaoCongNoEFToChiTietBaoCaoCongNoDTO(CHITIETBAOCAOCONGNO chitietbaocaocongnoEF)
       {
           Mapper.CreateMap<CHITIETBAOCAOCONGNO, ChiTietBaoCaoCongNoDTO>();
           return Mapper.Map<CHITIETBAOCAOCONGNO, ChiTietBaoCaoCongNoDTO>(chitietbaocaocongnoEF);
       }
       private static CHITIETBAOCAOCONGNO MapChiTietBaoCaoCongNoDTOToChiTietBaoCaoCongNoEF(ChiTietBaoCaoCongNoDTO chitietbaocaocongnoDTO)
       {
           Mapper.CreateMap<ChiTietBaoCaoCongNoDTO, CHITIETBAOCAOCONGNO>();
           return Mapper.Map<ChiTietBaoCaoCongNoDTO, CHITIETBAOCAOCONGNO>(chitietbaocaocongnoDTO);
       }
       public static List<ChiTietBaoCaoCongNoDTO> GetChiTietBaoCaoCongNoByMaBaoCaoCongNo(int mabaocaocongno)
       {
           List<ChiTietBaoCaoCongNoDTO> list = new List<ChiTietBaoCaoCongNoDTO>();
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   var query = from chitietbaocaocongno in context.CHITIETBAOCAOCONGNOList
                               where chitietbaocaocongno.MaBaoCaoCongNo ==mabaocaocongno
                               select chitietbaocaocongno;
                   foreach (var chitietbaocaocongnoEF in query)
                   {
                       ChiTietBaoCaoCongNoDTO chitietbaocaocongnoDTO = MapChiTietBaoCaoCongNoEFToChiTietBaoCaoCongNoDTO(chitietbaocaocongnoEF);
                       list.Add(chitietbaocaocongnoDTO);
                   }
               }
               catch (Exception)
               {
               }

           }
           return list;
       }
       public void Insert(ChiTietBaoCaoCongNoDTO chitietbaocaocongnoDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   CHITIETBAOCAOCONGNO chitietbaocaocongnoEF = new CHITIETBAOCAOCONGNO();
                   chitietbaocaocongnoEF = MapChiTietBaoCaoCongNoDTOToChiTietBaoCaoCongNoEF(chitietbaocaocongnoDTO);
                   context.AddToCHITIETBAOCAOCONGNOList(chitietbaocaocongnoEF);
                   context.SaveChanges();
               }
               catch (Exception)
               {
               }
           }
       }
       public void Update(ChiTietBaoCaoCongNoDTO chitietbaocaocongnoDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   EntityKey key = context.CreateEntityKey("CHITIETBAOCAOCONGNOList", new CHITIETBAOCAOCONGNO { MaChiTietBaoCaoCongNo = chitietbaocaocongnoDTO.MaChiTietBaoCaoCongNo });
                   CHITIETBAOCAOCONGNO origchitietbaocaocongno = context.GetObjectByKey(key) as CHITIETBAOCAOCONGNO;
                   context.Detach(origchitietbaocaocongno);
                   context.Attach(origchitietbaocaocongno);
                   CHITIETBAOCAOCONGNO updatechitietbaocaocongno = MapChiTietBaoCaoCongNoDTOToChiTietBaoCaoCongNoEF(chitietbaocaocongnoDTO);
                   context.ApplyCurrentValues<CHITIETBAOCAOCONGNO>("CHITIETBAOCAOCONGNOList", updatechitietbaocaocongno);
                   context.SaveChanges();
               }
               catch (Exception)
               {
               }
           }
       }
    }
}
